package com.wu.wechat.scheduled;

import com.wu.common.staticpack.Weixin;
import com.wu.common.utils.*;
import com.wu.entity.model.Data_style;
import com.wu.entity.model.WeixinTextTemplate;
import com.wu.entity.model.WeixinTextTemplateDetail;
import com.wu.entity.utils.DateUtils;
import com.wu.entity.weixin.po.*;
import com.wu.service.weixin.service.WeixinSignService;
import com.wu.service.weixin.service.WeixinTmessageService;
import com.wu.service.weixin.service.WxGzUserService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 未打卡通知提醒任务
 */
@Component
public class SignNotWortTask {
	public Logger LOG = LoggerFactory.getLogger(SignNotWortTask.class);
	@Autowired
	private WeixinSignService weixinSignService;
	@Autowired
	private WxGzUserService wxGzUserService;
	@Autowired
	private WeixinTmessageService weixinTmessageService;

	//第三方平台APPID
	private static String weixinId = Weixin.weixinId;


	@Scheduled(cron="0 21 * * * ?")
	public void run() {
		LOG.info("===================给未打卡签到的用户推送消息==========================");
		List<WeixinGzuser> gzList = wxGzUserService.queryAll(weixinId);
		try {
			List<WeixinSign> signsList = weixinSignService.queryAll(DateUtil.getLocalDate());
			List<String> tag = signsList.stream().map(WeixinSign::getOpenId).collect(Collectors.toList());
			List<String> result = new ArrayList<>();
			gzList.forEach(item->{
				boolean issign = false;
				for (int i = 0; i <tag.size() ; i++) {
					if(tag.get(i).equals(item.getOpenid())){
						issign = true;
						break;
					}
				}
				if(!issign){
					result.add(item.getOpenid());
				}
			});
			//TODO 调用公众号推送接口
			if(result.size()>0){
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
				String format = simpleDateFormat.format(new Date());
				Data_style first = new Data_style();
				Data_style keyword1 = new Data_style();
				Data_style keyword2 = new Data_style();
				Data_style remark = new Data_style();

				WeixinTextTemplate temp = new WeixinTextTemplate();
				WeixinTextTemplateDetail data = new WeixinTextTemplateDetail();

				first.setValue("您今天还未打卡签到，快去处理吧！！");
				first.setColor("#173177");

				keyword1.setValue("209人");
				keyword1.setColor("#173177");

				keyword2.setValue("29人");
				keyword2.setColor("#173177");

				remark.setValue("每个月累计三次未打卡，将会扣除奖励");
				remark.setColor("#173177");

				data.setFirst(first);
				data.setKeyword1(keyword1);
				data.setKeyword2(keyword2);
				data.setRemark(remark);
				temp.setUrl("www.baidu.com");
				temp.setTopcolor("#173177");
				temp.setTemplateId("EEBFXkMuRC1nQng7QvpnS-c63TXeK3SDZv9dmB5YJN4");//模板ID
				temp.setData(data);
				temp.setJwid(weixinId);
				Runnable run = new Runnable() {
					@Override
					public void run() {
						for (int i = 0; i < result.size(); i++) {
							temp.setTouser(result.get(i));
							JSONObject result = weixinTmessageService.sendTemplateMsg(temp);
							System.out.println(result);
						}
					}
				};
				new Thread(run).start();
			}

		} catch (Exception e) {
			LOG.info("给未打卡签到的用户推送消息的定时任务异常e={}",new Object[]{e});
		}
	}
	
	

}
